/*
 * @(#) LotInterService.java 2013-6-21 下午3:35:41
 *
 * Copyright 2013 Rockwell Automation, Inc. All rights reserved.
 * Rockwell Automation PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */package com.osi.mes.inter.wms;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import org.dom4j.DocumentException;

import pnuts.lang.Context;

import com.datasweep.compatibility.client.ATColumnDefinition;
import com.datasweep.compatibility.client.ATDefinition;
import com.datasweep.compatibility.client.ATHandler;
import com.datasweep.compatibility.client.ATRowBase;
import com.datasweep.compatibility.client.DependentATRow;
import com.datasweep.compatibility.client.Lot;
import com.datasweep.compatibility.client.ParentATRow;
import com.datasweep.compatibility.client.Part;
import com.datasweep.compatibility.client.Response;
import com.datasweep.compatibility.pnuts.Functions;
import com.datasweep.plantops.common.constants.IDataTypes;
import com.osi.mes.inter.BaseInterService;
import com.osi.mes.inter.erp.BomInterService;
import com.osi.mes.utils.DateTimeUtils;
import com.osi.mes.utils.StringUtils;
import com.rockwell.ftpc.framework.common.XMLUtils;
import com.rockwell.ftpc.framework.common.XmlData;

/**
 * 
 * 本接口用于和WMS同步物料信息
 * <p>
 * 
 * @author Paul, 2013-6-21 下午3:35:57
 */
public class LotInterService extends BaseInterService
{

	private static LotInterService instatnce = null;

	private LotInterService(Context context)
	{
		super(context);
	}

	private LotInterService(Functions functions)
	{
		super(functions);
	}

	public static LotInterService getInstance(Context context)
	{
		if (instatnce == null)
		{
			synchronized (LotInterService.class)
			{
				if (instatnce == null)
				{
					instatnce = new LotInterService(context);
				}
			}
		}
		return instatnce;
	}

	public String[] receiveLot(String xmlStr)
	{
		String[] results = new String[2];
		String result = "0";
		String remark = "";
		try
		{
			System.out.println(xmlStr);
			XmlData xmlData = XMLUtils.strToXMLData(xmlStr);
			Hashtable<String, ArrayList<Object>> value = xmlData.getData();
			String orderNumber = getTransferValue(
				value, INTER_WMS_LOT_ORDERNUMBER);
			ArrayList<Object> items = (ArrayList<Object>) value.get("item");
			for (int i = 0; i < items.size(); i++)
			{
				@SuppressWarnings("unchecked")
				Hashtable<String, ArrayList<Object>> item = (Hashtable<String, ArrayList<Object>>) items
					.get(i);
				String barcode = getTransferValue(
					item, INTER_WMS_LOT_BARCODE);
				String partNumber = getTransferValue(
					item, INTER_WMS_LOT_PARTNUMBER);
				String supplier = getTransferValue(
					item, INTER_WMS_LOT_SUPPLIER);
				if(StringUtils.isNull(supplier))
				{
					throw new Exception("供应商不允许为空");
				}
				String supplierLotNo = getTransferValue(
					item, INTER_WMS_LOT_SUPPLIERLOTNO);
				if(StringUtils.isNull(supplierLotNo))
				{
					throw new Exception("供应商批次不允许为空");
				}
				String lotQty = getTransferValue(
					item, INTER_WMS_LOT_QUANTITY);
				String productionDate = getTransferValue(item, "ProductionDate");
				String sapLotNo = getTransferValue(item, "SAPLotNo");
				Response response = createLot(
					orderNumber, barcode, partNumber, supplier, supplierLotNo,
					Double.parseDouble(lotQty),productionDate,sapLotNo);
				if (response != null && response.isError())
				{
					result = "1";
					remark = response.getFirstErrorMessage();
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
			result = "1";
			remark = e.getMessage();
		}
		results[0] = "<MessageResult><Status>"+result+"</Status><LastErrMsg>"+remark+"</LastErrMsg></MessageResult>";
		System.out.println(results[0]);
		return results;
	}

	/**
	 * 从WMS同步过来的已发料的物料信息,存储在Lot对象中
	 * 
	 * @param orderNumber
	 *            工单号
	 * @param barcode
	 *            托盘条码
	 * @param partNumber
	 *            物料编码
	 * @param supplier
	 *            供应商
	 * @param supplierLotNo
	 *            供应商批次
	 * @param lotQty
	 *            数量
	 * @param sapLotNo 
	 * @param productionDate 
	 * @return Lot 物料
	 */
	private Response createLot(String orderNumber, String barcode, String partNumber,
		String supplier, String supplierLotNo, double lotQty, String productionDate, String sapLotNo)
	{
		Response response = null;
		try
		{
			Part part = getFunctions().getPart(
				partNumber, "1");
			if (part == null)
			{
				return createErrorResponse(new Exception(
					"The part object did not exist, partNumber is " + partNumber));
			}
			BigDecimal lotQuantity = new BigDecimal(lotQty);
			Lot lot = getFunctions().getLotByName(
				barcode);
			if (lot != null)
			{
				response = lot.changeQuantity(lotQuantity);
				if (response.isError())
				{
					return response;
				}
			}
			else
			{
				response = getFunctions().receiveBulkLot(
					barcode, lotQuantity, part, false, null, null);
				if (response.isOk())
				{
					lot = (Lot) response.getResult();
				}
				else
				{
					return response;
				}
			}
			lot.changeToWIP();
			lot.setUDA(
				LOT_NOT_SCANED, UDA_LOT_IS_SCANNED);
			lot.setUDA(
				supplier, UDA_LOT_SUPPLIER);
			lot.setUDA(
				supplierLotNo, UDA_LOT_SUPPLIER_LOTNO);
			lot.setUDA(
				orderNumber, UDA_LOT_ORDER_NUMBER);
			lot.setDescription(part.getDescription());
			lot.setUDA(
				(String) part.getUDA(UDA_PART_ITEM_GROUP_NAME), UDA_LOT_MATERIAL_GROUP);
			lot.setUDA(
				(String) part.getUDA(UDA_PART_CLASS2), UDA_LOT_MATERIAL_TYPE);
			lot.setUDA(sapLotNo, UDA_LOT_SAP_LOT);
			if(StringUtils.isNotNull(productionDate))
			{
				lot.setUDT(DateTimeUtils.parseDateOfPnut(productionDate, "yyyy-MM-dd HH:mm:ss"),UDT_LOT_OUT_TIME);
			}
			response = lot.save();

		}
		catch (Exception e)
		{
			response = createErrorResponse(e);
		}
		return response;
	}
}
